package com.sctu.EMSApp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.bean.User;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;
import com.sctu.util.SharePreferenceUtil;

import java.util.HashMap;

/**
 * Created by zhu on 2016/1/5.
 */
public class RegisterActivity extends FragmentActivity implements View.OnClickListener{
    private EditText usernameEdit,passwordEdit,sureEdit;
    private ImageView registerBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setView();
        setEven();
    }

    private void setEven() {
        registerBtn.setOnClickListener(this);
    }

    private void setView() {
        usernameEdit = (EditText) findViewById(R.id.register_username);
        passwordEdit = (EditText) findViewById(R.id.register_passwords);
        sureEdit = (EditText) findViewById(R.id.register_sure_passwords);
        registerBtn = (ImageView) findViewById(R.id.my_register_botton);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_register_botton:
                String username = usernameEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                String sure = sureEdit.getText().toString();
                if (password.equals(sure)){
                    HashMap<String,String> loginMsg = new HashMap<String, String>();
                    loginMsg.put("username",username);
                    loginMsg.put("password",password);
                    GetVolleyHttp.getInstance(this).getStringPOSTJsonData(UrlConfig.URL_USER_REGISTER, loginMsg,listener,errorListener);
                }else {
                    Toast.makeText(RegisterActivity.this,"两次输入的密码不对",Toast.LENGTH_LONG).show();

                }

                break;
        }
    }

    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String s) {


            User userJsonListBean= JSON.parseObject(s, User.class);
            if (userJsonListBean.getCode() == 0){

                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                finish();
            }else {
                Toast.makeText(RegisterActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
            }


        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(RegisterActivity.this, "网络出错", Toast.LENGTH_LONG).show();
        }
    };
}
